package apis

import (
	"fmt"
	"mini-gde/pkg"
	"mini-gde/service"
	"net/http"

	"github.com/gin-gonic/gin"
)

// GetImages 镜像列表
func GetImages(c *gin.Context) {
	images := service.GetImages()
	c.JSON(http.StatusOK, gin.H{
		"code": pkg.SUCCESS,
		"msg":  pkg.GetMsg(pkg.SUCCESS),
		"data": images,
	})
}

func BuildDockerfile(c *gin.Context) {
	req := make(map[string][]string)
	c.BindJSON(&req)
	apps := req["apps"]

	var err error
	for _, app := range apps {
		fmt.Println(app)
		err = service.BuildImageByDockerfile(app)
	}
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": pkg.ERROR,
			"msg":  pkg.GetMsg(pkg.ERROR),
		})

	} else {

		c.JSON(http.StatusOK, gin.H{
			"code": pkg.SUCCESS,
			"msg":  pkg.GetMsg(pkg.SUCCESS),
		})
	}
}
func SaveImages(c *gin.Context) {
	req := make(map[string][]string)
	c.BindJSON(&req)
	apps := req["apps"]

	var err error
	service.SaveImage(apps, "/home/xfhuang/tmp.tar")
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": pkg.ERROR,
			"msg":  pkg.GetMsg(pkg.ERROR),
		})

	} else {

		c.JSON(http.StatusOK, gin.H{
			"code": pkg.SUCCESS,
			"msg":  pkg.GetMsg(pkg.SUCCESS),
		})
	}
}
func GetHarborImages(c *gin.Context) {
	images := service.GetHarborList()
	c.JSON(http.StatusOK, gin.H{
		"code": pkg.SUCCESS,
		"msg":  pkg.GetMsg(pkg.SUCCESS),
		"data": images,
	})
}
